package com.tanhua.manager.exception;

import com.tanhua.manager.vo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 *
 * @author liuyp
 * @date 2021/02/09
 */
@Slf4j
@ControllerAdvice
public class ExceptionAdvice {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseEntity handleExcepting(Exception e) {

        if (e instanceof BusinessException) {
            //把业务信息输出日志
            log.info(e.getMessage(), e);
            //返回响应
            return ResponseEntity.status(500).body(new CommonResult(e.getMessage()));
        } else {
            //把异常信息输出到堆栈
            e.printStackTrace();
            //返回响应
            return ResponseEntity.status(500).body(new CommonResult("服务器内部错误"));
        }
    }
}
